package com.bjsxt.users.service.impl;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.common.util.PageInfoUtil;
import com.bjsxt.users.dao.IARoleDao;
import com.bjsxt.users.dao.IAUsersDao;
import com.bjsxt.users.pojo.ARole;
import com.bjsxt.users.pojo.AUsers;
import com.bjsxt.users.service.IUsersService;

/**
 * 用户相关Service的实现类
 * @author wangshSxt
 *
 */
@Service("usersService")
public class UsersServiceImpl implements IUsersService
{
	@Resource
	private IAUsersDao usersDao;
	@Resource
	private IARoleDao roleDao;
	
	@Override
	public List<AUsers> findUsersCondListService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		List<AUsers> usersList = Collections.EMPTY_LIST;
		/* 为关键字加上% */
		if(condMap.get("keyword") != null)
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}
		if(pageInfoUtil != null)
		{
			condMap.put("pageCount", "true");
			condMap.put("currRecord", pageInfoUtil.getCurrRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			
			/* 查询总记录数 */
			usersList = this.usersDao.findCondList(condMap);
			/* 从List中拿数据为总记录数赋值
			 * List中肯定有结果:因为select count(*) 肯定返回一个数字
			 * id为什么是总记录数:因为有别名
			 *  */
			pageInfoUtil.setTotalRecord(usersList.get(0).getId());
			/* 查询分页内容 */
			condMap.put("pageCount", "false");
			usersList = this.usersDao.findCondList(condMap);
		}else
		{
			/* 不分页查询 */
			usersList = this.usersDao.findCondList(condMap);
		}
		return usersList;
	}

	@Override
	public AUsers findOneUsersService(Map<String, Object> condMap)
	{
		return this.usersDao.findOne(condMap);
	}

	@Override
	public JSONObject insertOneUsersService(AUsers users)
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.usersDao.insert(users);
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "添加成功");
			
			/* 放置二级JSON,影响类型 */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("effect", res);
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "添加失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneUsersService(AUsers users)
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.usersDao.update(users);
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "更新成功");
			
			/* 放置二级JSON,影响类型 */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("effect", res);
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "更新失败");
		}
		return resultJSON;
	}
	
	
	@Override
	public List<ARole> findRoleCondListService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		List<ARole> roleList = Collections.EMPTY_LIST;
		/* 为关键字加上% */
		if(condMap.get("keyword") != null)
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}
		if(pageInfoUtil != null)
		{
			condMap.put("pageCount", "true");
			condMap.put("currRecord", pageInfoUtil.getCurrRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			
			/* 查询总记录数 */
			roleList = this.roleDao.findCondList(condMap);
			/* 查询分页内容 */
			condMap.put("pageCount", "false");
			roleList = this.roleDao.findCondList(condMap);
		}else
		{
			/* 不分页查询 */
			roleList = this.roleDao.findCondList(condMap);
		}
		return roleList;
	}

	@Override
	public ARole findOneRoleService(Map<String, Object> condMap)
	{
		return this.roleDao.findOne(condMap);
	}

	@Override
	public JSONObject insertOneRoleService(ARole role)
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.roleDao.insert(role);
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "添加成功");
			
			/* 放置二级JSON,影响类型 */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("effect", res);
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "添加失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneRoleService(ARole role)
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.roleDao.update(role);
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "更新成功");
			
			/* 放置二级JSON,影响类型 */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("effect", res);
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "更新失败");
		}
		return resultJSON;
	}
}
